﻿using System;
using System.Collections.Generic;
using System.Text;
using Helper;
using com.SEO.Model;
using com.SEO.DataAccess;
using Helper.Bussiness;

namespace com.SEO.Business
{
    public class SiteManage : BaseManage
    {
        public int AddSiteInfo(SiteInfo info, string connName = ConnectionName)
        {

            SiteCtrl ctrl = new SiteCtrl();
            return ctrl.Insert(connName, info);
        }

        public int UpdateSiteInfo(SiteInfo info, string connName = ConnectionName)
        {

            SiteCtrl ctrl = new SiteCtrl();
            return ctrl.UpDate(connName, info);
        }

        public SiteInfo GetSiteInfoByID(string objectID, string connName = ConnectionName)
        {
            SiteCtrl ctrl = new SiteCtrl();
            List<SiteInfo> lstSiteInfo = ctrl.SelectAsList(connName, "SiteID = '" + objectID + "'");
            if (lstSiteInfo.Count > 0)
            {
                return lstSiteInfo[0];
            }

            return null;
        }

        public List<SiteInfo> GetSiteByCondition(string strCondition, string connName = ConnectionName)
        {
            SiteCtrl ctrl = new SiteCtrl();
            return ctrl.SelectAsList(connName, strCondition);
        }

        public void DeleteSiteInfo(SiteInfo info, string connName = ConnectionName)
        {
            SiteCtrl ctrl = new SiteCtrl();
            ctrl.Delete(connName, info.SiteId.ToString());
        }

        public void DeleteSiteInfoByID(string objectID, string connName = ConnectionName)
        {
            SiteCtrl ctrl = new SiteCtrl();
            ctrl.Delete(connName, objectID);
        }

        /// <summary>
        /// Check the |siteID| has been used or not.
        /// If not, then delete it.
        /// </summary>
        /// <param name="siteID"></param>
        public static void CheckSite(Guid siteID)
        {
            if (siteID == null)
                return;
            if (siteID.Equals(Guid.Empty))
                return;

            UserManage userManage = new UserManage();
            SiteManage siteManage = new SiteManage();
            KeywordManage keywordManage = new KeywordManage();
            List<UserInfo> lstUserInfo = userManage.GetUserByCondition(" SiteID = '" + siteID.ToString() + "'");

            if (lstUserInfo.Count == 0)
            {

                siteManage.DeleteSiteInfoByID(siteID.ToString());
                keywordManage.DeleteKeywardBySiteID(siteID.ToString());
            }
        }
    }
}
